package com.hnkywl.sts.action.sfgl;

import com.hnkywl.sts.action.flow.assign.RegectTaskSingleBackWayCmd;
import com.hnkywl.sts.dao.sfgl.SfkglDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.htgl.Xsht;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.flow.FlowService;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sfgl.FkshService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.jbpm.api.*;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：
 * 包:        com.hnkywl.sts.action.sfgl
 * 类描述:    岗位Action类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/sfgl/fksh")
public class FkshAction extends SimpleActionSupport {
    private String zt;

    private FkshService fkshService;

    private FlowService flowService;

    private ProcessEngine processEngine;

    private TaskService taskService;

    private ExecutionService executionService;

    private HistoryService historyService;

    private SfkglDao sfkglDao;
    //收付款实例
    private Sfk fksh;

    private Sfk bgfk;
    //付款单Id
    private Long fkshId;
    //单据号码
    private String bm;
    //申报公司机构名称
    private String sbgs;
    //申报公司机构Id
    private Long sbgsId;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    //关联开户行Id
    private Long cprKhhId;
    //票据id集合
    private String pjIds;
    private Long cghtId;

    private Long gsId;
    private Long zggsId;
    private Yg yg;

    private String xyzIds;

    private String fkshIds;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private GsjgService gsjgService;

    private String sysDate;

    private String gldwMc;

    private String fklx = "0";

    private String result;

    private String taskId;

    private String content;

    private String processInstanceId;

    private String executionId;

    private String sfje;

    private Long qtfk;

    private Date fkshStart;

    private Date fkshEnd;

    private String shrq;

    private String zdr;

    private Long glKhhId;


    //付款单的查询分页显示
    @Action("fkshList")
    public void fkshList() {
        this.renderJson(this.fkshService.findAllFksh(fkshStart, fkshEnd, bm, sbgs, sbgsId, gldwMc, zt, fklx,zdr, page, rows));
    }

    @Action("dbsfrwList")
    public void dbsfrwList() {
        zggsId = YgService.getCurrentYgZgGsId();
        this.renderJson(this.fkshService.findAllDbsfrw(zggsId));
    }

    //付款单的查询分页显示
    @Action("fkshByCghtList")
    public void fkshByCghtList() {
        this.renderJson(this.fkshService.findfkshByCghtList(cghtId, page, rows));
    }

    @Action("fksh")
    public String fksh() {
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        sysDate = simple.format(new Date());
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }


    @Action("fksh_xstk_content")
    public String fkshXstkContent() {
        //进入update页面
        yg = YgService.getCurrentYg();
        gsId = YgService.getCurrentYgGsId();
        zggsId = YgService.getCurrentYgZgGsId();
//        Long zgbmId = yg.getZgjg().getId();
        qtfk = 1L;
        if (fkshId != null) {
            fksh = fkshService.findFkshDetail(fkshId);
        } else {
            fksh = new Sfk();
            fksh.setSbr(yg);
            fksh.setSfklx(2);
            fksh.setSbgs((Nbgs) gsjgService.findGsjgDetail(zggsId));
//            fksh.setSbbm((Nbbm) gsjgService.findGsjgDetail(zgbmId));
        }
        return SUCCESS;
    }

    @Action("fksh_gyfk_content")
    public String fkshGyfkContent() {
        //进入update页面
        yg = YgService.getCurrentYg();
        gsId = YgService.getCurrentYgGsId();
        zggsId = YgService.getCurrentYgZgGsId();
//        Long zgbmId = yg.getZgjg().getId();
        qtfk = 0L;
        if (fkshId != null) {
            fksh = fkshService.findFkshDetail(fkshId);
        } else {
            fksh = new Sfk();
            fksh.setSbr(yg);
            fksh.setSfklx(1);
            fksh.setSbgs((Nbgs) gsjgService.findGsjgDetail(zggsId));
//            fksh.setSbbm((Nbbm) gsjgService.findGsjgDetail(zgbmId));
        }
        return SUCCESS;
    }


    @Action("fksh_detail")
    public String fkshDetail() {
        //进入update页面
        yg = YgService.getCurrentYg();
        gsId = YgService.getCurrentYgGsId();
        zggsId = YgService.getCurrentYgZgGsId();
        Long zgbmId = yg.getZgjg().getId();
        if (fkshId != null) {
            fksh = fkshService.findFkshDetail(fkshId);
        } else {
            fksh = new Sfk();
            fksh.setSbr(yg);
            fksh.setSbgs((Nbgs) gsjgService.findGsjgDetail(zggsId));
            fksh.setSbbm((Nbbm) gsjgService.findGsjgDetail(zgbmId));
        }
        return SUCCESS;
    }

    /**
     * 方法名:     addFksh
     * 功能描述:      增加付款单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-29上午9:47
     * 修改人:     zsp
     * 修改日期:   13-3-29上午9:47
     */
    @Action("add_fksh")
    public void addFksh() {
        try {
            fksh.setQtfk(Integer.getInteger(qtfk.toString()));
            if (qtfk == 0L) {
                if(fksh.getGldw() ==null){
                    renderText("关联单位不可为空");
                }
                if (this.fkshService.checkAddFkshJe(fksh)) {
                    this.fkshService.saveFksh(fksh);
                    renderText("添加付款单成功!");
                } else {
                    renderText("您的借款数超标!");
                }
            } else {
                this.fkshService.saveTkFksh(fksh, sfje);
                renderText("添加付款单成功!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    @Action("add_bgfksh")
    public void addBgfksh() {
        try {
            this.fkshService.saveBgFksh(bgfk);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加保供付款成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     updateFksh
     * 功能描述:      修改付款单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     scl
     * 创建日期:   13-3-29上午11:50
     * 修改人:     scl
     * 修改日期:   13-3-29上午11:50
     */
    @Action("update_fksh")
    public void updateFksh() {
        try {
            if (fksh.getQtfk() == 0) {
                this.fkshService.updateFksh(fksh);
            } else {
                this.fkshService.updateTKFksh(fksh, sfje);
            }
            renderText("修改付款单成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }


    @Action("update_bgfksh")
    public void updateBgfksh() {
        try {
            this.fkshService.updateBgFksh(bgfk);
            renderText("修改保供付款单成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    /**
     * 方法名:     deleteFksh
     * 功能描述:      删除数据库付款单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     zsp
     * 创建日期:   13-3-29上午11:47
     * 修改人:     zsp
     * 修改日期:   13-3-29上午11:47
     */
    @Action("delete_fksh")
    public void deleteFksh() {
        try {
            this.fkshService.deleteFksh(fkshId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除付款单成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("fkshByProcessInstance")
    public void fkshByProcessInstance() {
        Sfk sfk;
        String processInstanceId;
        Execution execution = this.executionService.findExecutionById(executionId);
        if (execution != null) {
            sfk = this.fkshService.findSfkDetailByProcessInstance(execution.getProcessInstance().getId());
            processInstanceId = execution.getProcessInstance().getId();
        } else {
            sfk = this.fkshService.findSfkDetailByProcessInstance(executionId);
            processInstanceId = executionId;
        }
        String xyJson = "{'id':'" + sfk.getId() + "','businessNo':'" + sfk.getBm() + "','processInstanceId':'" + processInstanceId + "'}";
        this.renderText(xyJson);
    }

    /**
     * 远程ajax验证
     */
    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!fkshService.ajaxValidate(fkshId, validateField, validateVal))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    //修改付款单状态
    @Action("change_status")
    public void changeStatus() {
        try {
            fkshService.changeStatus(fkshIds, zt);
            renderText("付款单已审核!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    //修改内部划转单的付款单状态
    @Action("change_status_nbhz")
    public void changeStatusNbhz() {
        try {
            fkshService.changeStatusNbhz(fkshIds, zt);
            renderText("付款单已审核!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    @Action("fksh_tj")
    public void fkshTj() {
        try {
            fkshService.FkshTj(fkshIds, zt);
            renderText("付款单已提交!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    @Action("fksh_th")
    public void fkshTh() {
        try {
            fkshService.FkshTh(fkshId, zt);
            renderText("付款单已退回!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    @Action("fksh_qxsh")
    public void fkshQxsh() {
        try {
            if (this.sfkglDao.qxshyz(fkshId)) {
                fkshService.FkshQxsh(fkshId, zt);
                renderText("付款单已取消审核!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }


    @Action("fksh_sh_pj") //银票和商票的付款单
    public String fkshShPj() {
        fksh = this.fkshService.findFkshDetail(fkshId);
        return SUCCESS;
    }

    @Action("fksh_sh_xyz") //信用证的付款单
    public String fkshShXyz() {
        fksh = this.fkshService.findFkshDetail(fkshId);
        return SUCCESS;
    }

    @Action("fksh_sh") //其它收付类型的付款单
    public String fkshSh() {
        fksh = this.fkshService.findFkshDetail(fkshId);
        return SUCCESS;
    }

    @Action("check_fksh")
    public void checkFksh() {
        try {
            this.fkshService.checkFksh(fksh.getId(), cprKhhId,glKhhId);
            renderText("应付款已完成入账!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    @Action("check_fksh_pj")
    public void checkFkshPj() {
        try {
            this.fkshService.checkFkshPj(fksh.getId(), pjIds);
            renderText("应付款已完成入账!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }

    @Action("check_fksh_xyz")
    public void checkFkshXyz() {
        try {
            this.fkshService.checkFkshXyz(fksh.getId(), xyzIds);
            renderText("应付款已完成入账!");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage());
        }
    }


    @Action(value = "examineTask")
    public void examineTask() {
        if (this.result.equals("审核")) {
            Map<String, Object> var = new HashMap<String, Object>();
            var.put("owner", YgService.getCurrentYg().getId());
            var.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
            var.put("result", "审核通过");
            var.put("content", content);
            var.put("time", new Date());
            Task currentTask = this.taskService.getTask(taskId);
            String executionId = currentTask.getExecutionId();
            executionService.createVariables(executionId, var, true);

            Execution execution = this.executionService.findExecutionById(executionId);
            Sfk sfk = fkshService.findSfkDetailByProcessInstance(execution.getProcessInstance().getId());
            var.put("status", result);
            var.put("contain", StringUtils.EMPTY);
            this.taskService.completeTask(taskId, var);
            if (sfk.getZt().equals("ybc")) {
                sfk.setZt("ytj");
                fkshService.updateFksh(sfk);
            }
        } else {
            String result = processEngine.execute(new RegectTaskSingleBackWayCmd(taskId, content));
            if (result.equals("change")) {
                //修改状态  ytj--->ybc
                String executionId = this.historyService.createHistoryTaskQuery().taskId(taskId).uniqueResult().getExecutionId();
                Execution execution = this.executionService.findExecutionById(executionId);
                Sfk sfk = fkshService.findSfkDetailByProcessInstance(execution.getProcessInstance().getId());
                sfk.setZt("ybc");
                fkshService.updateFksh(sfk);
            }
        }
        this.renderText("{\"statusCode\":\"200\", \"message\":\"审核流程成功\"}");
    }

    @Action("to_fksh_xgshsj")
    public String bgjhywk_yq() {
        fksh = this.fkshService.findFkshDetail(fkshId);
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        if(fksh.getShrq() !=null )
            shrq= simple.format(fksh.getShrq());
        return SUCCESS;
    }

    @Action("fksh_xgshsj")
    public void editCght() {
        try {
            this.fkshService.xgshrq(fksh);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改审核时间成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("fksh_tx_content")
    public String fksh_tx_content(){
        fksh = new Sfk();
        fksh.setSbgs(YgService.getCurrentYg().getZgjg());
        fksh.setSbr(YgService.getCurrentYg());
        return SUCCESS;
    }
    //提现
    @Action("fksh_tx")
    public void fkshTx() {
        try {
            this.fkshService.applyCash(fksh);
            renderText("{\"statusCode\":\"200\", \"message\":\"操作成功！\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    //=============================GetterandSetter============================================

    public String getBm() {
        return bm;
    }

    public void setBm(String bm) {
        this.bm = bm;
    }

    public String getSbgs() {
        return sbgs;
    }

    public void setSbgs(String sbgs) {
        this.sbgs = sbgs;
    }

    public Long getFkshId() {
        return fkshId;
    }

    public void setFkshId(Long fkshId) {
        this.fkshId = fkshId;
    }

    public Sfk getFksh() {
        return fksh;
    }

    public void setFksh(Sfk fksh) {
        this.fksh = fksh;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public Long getCghtId() {
        return cghtId;
    }

    public void setCghtId(Long cghtId) {
        this.cghtId = cghtId;
    }

    public Long getCprKhhId() {
        return cprKhhId;
    }

    public void setCprKhhId(Long cprKhhId) {
        this.cprKhhId = cprKhhId;
    }

    public String getPjIds() {
        return pjIds;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getZggsId() {
        return zggsId;
    }

    public void setZggsId(Long zggsId) {
        this.zggsId = zggsId;
    }

    public void setPjIds(String pjIds) {
        this.pjIds = pjIds;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getXyzIds() {
        return xyzIds;
    }

    public void setXyzIds(String xyzIds) {
        this.xyzIds = xyzIds;
    }

    public String getSysDate() {
        return sysDate;
    }

    public void setSysDate(String sysDate) {
        this.sysDate = sysDate;
    }

    public String getGldwMc() {
        return gldwMc;
    }

    public void setGldwMc(String gldwMc) {
        this.gldwMc = gldwMc;
    }

    public Sfk getBgfk() {
        return bgfk;
    }

    public void setBgfk(Sfk bgfk) {
        this.bgfk = bgfk;
    }

    public String getFkshIds() {
        return fkshIds;
    }

    public void setFkshIds(String fkshIds) {
        this.fkshIds = fkshIds;
    }

    public String getFklx() {
        return fklx;
    }

    public void setFklx(String fklx) {
        this.fklx = fklx;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getProcessInstanceId() {
        return processInstanceId;
    }

    public void setProcessInstanceId(String processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    public String getExecutionId() {
        return executionId;
    }

    public void setExecutionId(String executionId) {
        this.executionId = executionId;
    }

    public String getSfje() {
        return sfje;
    }

    public void setSfje(String sfje) {
        this.sfje = sfje;
    }

    public Long getQtfk() {
        return qtfk;
    }

    public void setQtfk(Long qtfk) {
        this.qtfk = qtfk;
    }

    public Date getFkshStart() {
        return fkshStart;
    }

    public void setFkshStart(Date fkshStart) {
        this.fkshStart = fkshStart;
    }

    public Date getFkshEnd() {
        return fkshEnd;
    }

    public void setFkshEnd(Date fkshEnd) {
        this.fkshEnd = fkshEnd;
    }

    public String getShrq() {
        return shrq;
    }

    public void setShrq(String shrq) {
        this.shrq = shrq;
    }

    public String getZdr() {
        return zdr;
    }

    public void setZdr(String zdr) {
        this.zdr = zdr;
    }

    public Long getGlKhhId() {
        return glKhhId;
    }

    public void setGlKhhId(Long glKhhId) {
        this.glKhhId = glKhhId;
    }

    @Autowired
    public void setFkshService(FkshService fkshService) {
        this.fkshService = fkshService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
    public void setSfkglDao(SfkglDao sfkglDao) {
        this.sfkglDao = sfkglDao;
    }

    @Autowired
    public void setFlowService(FlowService flowService) {
        this.flowService = flowService;
    }

    @Autowired
    public void setExecutionService(@Qualifier("executionService") ExecutionService executionService) {
        this.executionService = executionService;
    }

    @Autowired
    public void setTaskService(@Qualifier("taskService") TaskService taskService) {
        this.taskService = taskService;
    }

    @Autowired
    public void setProcessEngine(@Qualifier("processEngine") ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    @Autowired
    public void setHistoryService(@Qualifier("historyService") HistoryService historyService) {
        this.historyService = historyService;
    }
}
